package com.ytjj.qmyx.users.controller.qyim;

import com.alibaba.fastjson.JSON;
import com.ytjj.common.service.RedisServiceImpl;
import com.ytjj.qmyx.users.model.request.QyImUserReq;
import com.ytjj.qmyx.users.model.request.QyOrderReq;
import com.ytjj.qmyx.users.model.response.QyOrdersResponse;
import com.ytjj.qmyx.users.service.QiYuService;
import com.ytjj.qmyx.users.utils.GetTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/qyim")
@Slf4j
public class QiyuController {

    private static final String APPID = "qyd7e614a2afe262a6";
    private static final String APPSECRET = "ED1FE3A6D7E614A2AFE262A6BAC9CF22";

    private static final String QYTOKEN = "im:qy:token";

    @Resource
    private RedisServiceImpl redisService;

    @Resource
    private QiYuService qiYuService;


    /**
     * 获取 token
     * @param appid
     * @param appsecret
     * @return
     */
    @GetMapping("/get_token")
    public Map<String,Object> getToken(String appid,String appsecret){
        Map<String, Object> map = new HashMap<>();
        if (APPID.equals(appid) && APPSECRET.equals(appsecret)){
            String token = redisService.get(QYTOKEN);
            if (StringUtils.isBlank(token)){
                token = GetTokenUtil.getToken();
                redisService.set(QYTOKEN,token);
            }
            redisService.expire(token, 60 * 60 * 24);
            map.put("rlt",0);
            map.put("token",token);
            map.put("expires",60 * 60 * 24);
        }else {
            map.put("rlt",1);
            map.put("msg","认证不通过");
        }
        return map;
    }

    /**
     * 获取用户信息
     * @param qyImUserReq
     * @return
     */
    @PostMapping("/get_call_user_info")
    public Map<String,Object> getCallUserInfo(@RequestBody QyImUserReq qyImUserReq){
        Map<String, Object> map = new HashMap<>();
        if (!APPID.equals(qyImUserReq.getAppid())){
            map.put("rlt",1);
            map.put("msg","请提供正确的appid");
            return map;
        }
        String token = redisService.get(QYTOKEN);
        if (token==null || qyImUserReq.getToken()==null || !qyImUserReq.getToken().equals(token)){
            map.put("rlt",2);
            map.put("msg","token已过期");
            return map;
        }
        return qiYuService.getCallUserInfo(qyImUserReq.getUserid());
    }

    /**
     * 在线会话获取用户订单信息
     * @param qyOrderReq
     * @return
     */
    @PostMapping("/get_order")
    public QyOrdersResponse getOrder(@RequestBody QyOrderReq qyOrderReq){
        log.info("====在线会话，获取当前用户订单==="+ JSON.toJSONString(qyOrderReq));
        QyOrdersResponse response = new QyOrdersResponse();
        if (!APPID.equals(qyOrderReq.getAppid())){
            response.setRlt(1);
            return response;
        }
        String token = redisService.get(QYTOKEN);
        if (token==null || qyOrderReq.getToken()==null || !qyOrderReq.getToken().equals(token)){
            response.setRlt(2);
            return response;
        }
        return qiYuService.getOrderFromQy(qyOrderReq);
    }


    /**
     * 呼叫中心获取用户订单信息
     * @param qyOrderReq
     * @return
     */
    @PostMapping("/get_call_order")
    public QyOrdersResponse getCallOrder(@RequestBody QyOrderReq qyOrderReq){
        log.info("====呼叫中心，获取当前用户订单==="+ JSON.toJSONString(qyOrderReq));
        QyOrdersResponse response = new QyOrdersResponse();
        if (!APPID.equals(qyOrderReq.getAppid())){
            response.setRlt(1);
            return response;
        }
        String token = redisService.get(QYTOKEN);
        if (token==null || qyOrderReq.getToken()==null || !qyOrderReq.getToken().equals(token)){
            response.setRlt(2);
            return response;
        }
        return qiYuService.getOrderFromQyCall(qyOrderReq);
    }
}
